﻿package com.firewolf.observer.impl;

import java.util.ArrayList;

import com.firewolf.observer.itf.IObserver;
import com.firewolf.observer.itf.IObservable;

/**
 * @author liuxing0
 * @function 气象数据，可观察者
 * @createtime 2014-8-19
 */
public class WeatherData implements IObservable{
	private ArrayList<IObserver> observers;
	private float temperature;//温度
	private float humidity; //湿度
	private float pressure; //气压
	public WeatherData() {
		observers = new ArrayList<>();
	}
	@Override
	public void registerObserver(IObserver observer) {
		observers.add(observer);
	}
	@Override
	public void removeObserver(IObserver observer) {
		int index = observers.indexOf(observer);
		if(index >= 0)
			observers.remove(index);
	}
	
	@Override
	public void notifyObservers() {
		for(int i = 0;i<observers.size();i++){
			IObserver observer = observers.get(i);
			observer.update(temperature,humidity,pressure);
		}
	}
	
	public void measurementsChanged(){
		notifyObservers();
	}

	public void setMeasurements(float temperature,float humidity,float pressure){
		this.temperature = temperature;
		this.humidity = humidity;
		this.pressure = pressure;
		measurementsChanged();
	}
}
